package com.qf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.event.util.EventUtil;
import com.qf.aop.UserLocal;
import com.qf.dao.OrdersDao;
import com.qf.entity.*;
import com.qf.feign.RoomFeign;
import com.qf.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl extends ServiceImpl<OrdersDao,Orders> implements IOrderService {
    @Autowired
    private RoomFeign roomFeign;
    @Autowired
    private EventUtil eventUtil;

    @Override
    public PricesResult getPrices(PricesParams pricesParams) {
        R<Room> roomResult = roomFeign.getRoomById(pricesParams.getRid());

        Room room = roomResult.getData();

        List<RoomPrice> prices = room.getPrices();

        BigDecimal allPrices = BigDecimal.valueOf(0);

        List<List<String>> priceDetails = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        for (int i = 0; i < prices.size(); i++) {
            if (prices.get(i).getDate().getTime() >=pricesParams.getBeginTime().getTime()
                    && prices.get(i).getDate().getTime()<pricesParams.getEndTime().getTime()){

                if (prices.get(i).getHasNumber() - prices.get(i).getNumber() < pricesParams.getRnumber()){
                    return new PricesResult().setCode(1);
                }

                BigDecimal priceDay = BigDecimal.valueOf(prices.get(i).getPrice()).multiply(BigDecimal.valueOf(pricesParams.getRnumber()));

                allPrices = allPrices.add(priceDay);

                List<String> list = new ArrayList<>();
                list.add(simpleDateFormat.format(prices.get(i).getDate()));
                list.add(pricesParams.getRnumber() + "X" + prices.get(i).getPrice());
                priceDetails.add(list);
            }
        }
        PricesResult result = new PricesResult()
                .setAllPrice(allPrices.doubleValue())
                .setPricesDetails(priceDetails);

        return result;
    }

    @Override
    @Transactional
    public String createOrders(Orders orders, PricesParams pricesParams) {
        User user = UserLocal.getUser();

        PricesResult prices =  this.getPrices(pricesParams);

        int days = (int) ((orders.getEndTime().getTime() - orders.getBeginTime().getTime()) / 1000 / 60 / 60 / 24 );

        String oid = UUID.randomUUID().toString();

        orders
                .setOid(oid)
                .setUid(user.getId())
                .setNumber(pricesParams.getRnumber())
                .setAllPrice(prices.getAllPrice())
                .setDays(days);

        super.save(orders);

        eventUtil.publishEvent("create_orders",pricesParams);

        return oid;
    }
}
